package com.itcast.demo.thread_test.controller;

import com.itcast.demo.mapper.EmployeeMapper;
import com.itcast.demo.pojo.Employee;
import com.itcast.demo.thread_test.service.AsyncService;
import com.itcast.demo.thread_test.service.impl.AsyncServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.ASTList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@Slf4j
@RestController
@RequestMapping("/thread")
public class ThreadTestController {

    @Autowired
    private AsyncService asyncService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @PostMapping("/adds")
    public void addEmployees() {
        List<Employee> employees = Arrays.asList(
                new Employee(1L, "张三3", 1),
                new Employee(112L, "李四x", 0),
                new Employee(113L, "王五w", 1),
                new Employee(114L, "赵六s", 0),
                new Employee(115L, "孙七x", 1),
                new Employee(116L, "张三d", 1),
                new Employee(117L, "李四e3", 0),
                new Employee(118L, "王五43", 1),
                new Employee(119L, "赵六ea", 0),
                new Employee(200L, "孙七hr", 1),

                new Employee(2L, "32张三3", 1),
                new Employee(212L, "ew李四x", 0),
                new Employee(213L, "ffs王五w", 1),
                new Employee(214L, "fs赵六s", 0),
                new Employee(215L, "vd孙七x", 1),
                new Employee(216L, "vcn张三d", 1),
                new Employee(217L, "b李vn四e3", 0),
                new Employee(218L, "m王五43", 1),
                new Employee(219L, "n,赵六ea", 0),
                new Employee(201L, "jiu孙七hr", 1)
        );
        CountDownLatch countDownLatch = new CountDownLatch(employees.size());
        asyncService.executeAsync(employees,employeeMapper,countDownLatch);

        try {
            countDownLatch.await();  // 保证之前的所有的线程都执行完成，再进行后续操作
        } catch (InterruptedException e) {
            log.error("等待过程中发生异常"+ e.getMessage());
            e.printStackTrace();
        }
    }
}
